package bdddaosimpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import bdd.beans.DBLink;
import bdd.beans.Indisponibilites;
import bdd.beans.Jours;
import bdd.beans.Ue;
import bdddaos.DAOUe;
import bdddaos.DAOUe;

public class DAOUeImpl implements DAOUe {
	private DBLink dblink;
	
	public DAOUeImpl() {
		super();
		this.dblink = new DBLink();
	}

	public Ue save(Ue o) {
		try {
			this.dblink.connexion();
			String s = "INSERT INTO UE (NUMSEMESTRE, NUMFORMATION)" +
				" VALUES (" + o.getSemestre().getNumsemestre() +
				", " + o.getFormation().getNumformation() + ")";
			this.dblink.setPst(this.dblink.getConnection().prepareStatement(s));
			this.dblink.getPst().executeQuery();
			DAOOutils outil = new DAOOutils();
			o.setNumue(outil.findMaxId("UE", "NUMUE"));
		}
		catch(SQLException se) {
			System.out.println("Erreur de requete : " + se);
		}
		finally{
			this.dblink.deconnexion();
		}
		return o;
	}

	public Ue find(Integer numUe) {
		Ue retour = new Ue();
		retour.setNumue(numUe);
		try {
			this.dblink.connexion();
			String s = "SELECT * FROM UE WHERE NUMUE = " + numUe;
			this.dblink.setPst(this.dblink.getConnection().prepareStatement(s));
			ResultSet rs = this.dblink.getPst().executeQuery();
			while(rs.next()) {
				retour.setNumue(rs.getInt("NUMUE"));
				retour.setSemestre(new DAOSemestreImpl().find(rs.getInt("NUMSEMESTRE")));
				retour.setFormation(new DAOFormationImpl().find(rs.getInt("NUMFORMATION")));
				
				retour.setEcs(null);
			}
		}
		catch(SQLException se) {
			System.out.println("Erreur de requete : " + se);
		}
		finally{
			this.dblink.deconnexion();
		}
		return retour;
	}

	public List<Ue> findAll() {
		List<Ue> listeRetour = new ArrayList();
		try {
			this.dblink.connexion();
			this.dblink.setPst(this.dblink.getConnection().prepareStatement("SELECT * FROM UE"));
			ResultSet rs = this.dblink.getPst().executeQuery();
			while(rs.next()) {
				Ue retour = new Ue();
				retour.setNumue(rs.getInt("NUMUE"));
				retour.setSemestre(new DAOSemestreImpl().find(rs.getInt("NUMSEMESTRE")));
				retour.setFormation(new DAOFormationImpl().find(rs.getInt("NUMFORMATION")));
				listeRetour.add(retour);
			}
		}
		catch(SQLException se) {
			System.out.println("Erreur de requete : " + se);
		}
		finally {
			this.dblink.deconnexion();
		}
		return listeRetour;
	}

	@Override
	public void update(Ue o) {
		try {
			this.dblink.connexion();
			String s = "UPDATE UE SET NUMSEMESTRE=" + o.getSemestre().getNumsemestre() + ", NUMFORMATION="+ o.getFormation().getNumformation() + " WHERE NUMUE=" + o.getNumue();
			
			this.dblink.setPst(this.dblink.getConnection().prepareStatement(s));
			this.dblink.getPst().executeQuery();
		}
		catch(SQLException se) {
			System.out.println("Erreur de requete : " + se);
		}
		finally {
			this.dblink.deconnexion();
		}
	}

	@Override
	public void delete(Ue o) {
		try {
			this.dblink.connexion();
			String s = "DELETE FROM UE WHERE NUMUE=" + o.getNumue();
			
			this.dblink.setPst(this.dblink.getConnection().prepareStatement(s));
			this.dblink.getPst().executeQuery();
		}
		catch(SQLException se) {
			System.out.println("Erreur de requete : " + se);
		}
		finally{
			this.dblink.deconnexion();
		}
	}
}
